package com.polaris.live.widget.load

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Point
import android.graphics.Shader
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator

/**
 * HaloView
 * @author Created by 天晴 on 2024/1/20 14:02
 * @since 1.0.0
 **/
class HaloView(context: Context, attrs: AttributeSet?) : View(context, attrs) {

    private val mPaint = Paint()
    private val mPath = Path()

    private var mLinearGradient: LinearGradient? = null
    private var mValueAnimator: ValueAnimator? = null

    private fun initPointAndAnimator(w: Int, h: Int) {
        val point1 = Point(0, 0)
        val point2 = Point(w, 0)
        val point3 = Point(w, h)
        val point4 = Point(0, h)

        mPath.moveTo(point1.x.toFloat(), point1.y.toFloat())
        mPath.lineTo(point2.x.toFloat(), point2.y.toFloat())
        mPath.lineTo(point3.x.toFloat(), point3.y.toFloat())
        mPath.lineTo(point4.x.toFloat(), point4.y.toFloat())
        mPath.close()

        // 斜率k
        val k = 1f * 0
        // 偏移
        val offset = 1f * w / 2
        mValueAnimator?.cancel()
        mValueAnimator = ValueAnimator.ofFloat(0f - offset * 2, w + offset * 2).apply {
            interpolator = LinearInterpolator()
            duration = 2000
            repeatCount = ValueAnimator.INFINITE
            addUpdateListener { animation ->
                val value = animation.animatedValue as Float
                mLinearGradient = LinearGradient(
                    value,
                    k * value,
                    value + offset,
                    k * (value + offset),
                    intArrayOf(
                        Color.parseColor("#00FFFFFF"),
                        Color.parseColor("#44FFFFFF"),
                        Color.parseColor("#00FFFFFF")
                    ),
                    null,
                    Shader.TileMode.CLAMP
                )
                mPaint.shader = mLinearGradient
                invalidate()
            }
        }
        start()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthSize: Int = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize: Int = MeasureSpec.getSize(heightMeasureSpec)
        initPointAndAnimator(widthSize, heightSize)
    }

    // 开启动画
    fun start() {
        val valueAnimator = mValueAnimator
        if (valueAnimator != null && !valueAnimator.isRunning) {
            valueAnimator.start()
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawPath(mPath, mPaint)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        start()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mValueAnimator?.cancel()
        mValueAnimator = null
    }
}
